package com.gmrz.util;

import java.nio.charset.Charset;
import org.apache.commons.codec.binary.Base64;

/**
 * Created by gmrz-ops on 2017/12/11.
 */
public class Base64Utils {
    //private static final Base64 BASE64 = new Base64();
    private static final Charset DEFAULT_CHARSET = Charset.forName("UTF-8");

    public static String encode(byte[] source) {
        byte[] bytes = Base64.encodeBase64(source);
        return new String(bytes, DEFAULT_CHARSET);
    }

    public static byte[] decode(String source) {
        if (!isEmpty(source)) {
            byte[] bytes = Base64.decodeBase64(source.getBytes(DEFAULT_CHARSET));
            return bytes;
        }

        return null;
    }

    private static boolean isEmpty(String str) {
        return str == null || str.length() == 0;
    }
    public static void main(String[] args) {
        String target = Base64Utils.encode("admin".getBytes());

        byte[] source = Base64Utils.decode("YWRtaW4=");

    }

}
